/**
 *
 */
package com.pocketsweet.ui.uilib;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.pocketsweet.R;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.PictureViewActivity;
import com.pocketsweet.utils.PhotoUtil;

/**
 * @author bobhu 八张图最牛逼
 */
public class BaZhangTuView extends LinearLayout {
    // 用来显示图片
    public ImageLoader imageLoader = ImageLoader.getInstance();
    // 保存图片的URL，如果是本地文件请在前面增加"file://"
    private ArrayList<String> mImagePath = new ArrayList<String>();
    private ArrayList<String> mBigImagePath = new ArrayList<String>();
    private LinearLayout llRow2;
    private ArrayList<ImageView> mImageViews;

    public BaZhangTuView(Context context) {
        super(context);
        View root = inflate(context, R.layout.view_bazhangtu, null);
        int W = ((Activity) context).getWindowManager().getDefaultDisplay()
                .getWidth();// 获取屏幕高度
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, W / 4);
        llRow2 = (LinearLayout) root.findViewById(R.id.llRow2);
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView1));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView2));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView3));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView4));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView5));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView6));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView7));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView8));
        addView(root, params);
    }

    public BaZhangTuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View root = inflate(context, R.layout.view_bazhangtu, null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        llRow2 = (LinearLayout) root.findViewById(R.id.llRow2);
        // 获取控件哦
        mImageViews = new ArrayList<ImageView>();
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView1));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView2));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView3));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView4));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView5));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView6));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView7));
        mImageViews
                .add((ImageView) root.findViewById(R.id.ivImageView8));
        addView(root, params);
    }

    public void setImage() {
        // issus 当图片的大小小于3的时候就不显示了?
        // fixed bobhu
        if (mImagePath.size() <= 4) {
            // 第二行不显示
            llRow2.setVisibility(View.GONE);
        } else {
            llRow2.setVisibility(View.VISIBLE);
        }
        // CODE REVIEW mImagePath.size()如果大于8呢？是否进行了多次循环，这时候是否会报错?
        // 空指针，因为mImageViews可没有8个哦
        // fixed bobhu
        int end = mImagePath.size() < 8 ? mImagePath.size() : 8;
        for (int i = 0; i < end; i++) {
            mImageViews.get(i).setVisibility(View.VISIBLE);
//			 Bitmap bmp = null;
//			 bmp = ImageLoader.getInstance().loadImageSync(mImagePath.get(i));
//			 mImageViews.get(i).setImageBitmap(bmp);
            // final int temp = i;
            // ImageLoader.getInstance().loadImage(mImagePath.get(i),
            // PhotoUtil.squareImageOptions1,
            // new SimpleImageLoadingListener() {
            // public void onLoadingComplete(String imageUri,
            // android.view.View view,
            // android.graphics.Bitmap loadedImage) {
            // mImageViews.get(temp).setImageBitmap(loadedImage); //
            // imageView，你要显示的imageview控件对象，布局文件里面//配置的
            // };
            // });

            UserService.displaySquare(mImagePath.get(i), mImageViews.get(i));
//			imageLoader.displayImage(mImagePath.get(i), ),
//					PhotoUtil.squareImageOptions);
            final int num = i;
            mImageViews.get(i).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(),
                            PictureViewActivity.class);
                    intent.putStringArrayListExtra("ImageList", mBigImagePath);
                    intent.putExtra("index", num);
                    getContext().startActivity(intent);
                }
            });
        }
        for (int j = end; j < 8; j++) {
            mImageViews.get(j).setVisibility(View.INVISIBLE);
        }
    }

    public ArrayList<String> getImagePaths() {
        return mImagePath;
    }

    public void setImagePaths(ArrayList<String> path) {
        mImagePath = path;
    }

    public ArrayList<String> getBigImagePaths() {
        return mBigImagePath;
    }

    public void addImagePath(String sPath) {
        if (mImagePath != null && !mImagePath.contains(sPath)) {
            mImagePath.add(sPath);
        }
    }

    public void removeImagePath(int index) {
        mImagePath.remove(index);
    }

    public void addBigImagePath(String Path) {
        if (!mBigImagePath.contains(Path)) {
            mBigImagePath.add(Path);
        }
    }
}
